package com.hanhanxiaochong.zhongxiangchuanmei.presenter;

import com.hanhanxiaochong.zhongxiangchuanmei.bean.BannerBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.MemberShipBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.SystemNoticeBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.base.BasePresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IHomeView;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseApi;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.exception.ApiException;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by RockPhoenix on 2018/5/16.
 */

public class HomePresenter extends BasePresenter<IHomeView> {

    public HomePresenter(IHomeView iView) {
        super(iView);
    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi) {
        HttpManager manager = new HttpManager(new HttpOnNextListener() {
            @Override
            public void onNext(String resulte, String mothead) {

            }

            @Override
            public void onError(ApiException e) {

            }
        }, rxAppCompatActivity);
        manager.doHttpDeal(baseApi);
    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi, int state) {

    }

    public void getHot(){
        addSubscription(RetrofitSource.createApi(HttpPostService.class).membership()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<MemberShipBean>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<MemberShipBean> response) {
                        iView.setHot(response.getData());
                    }
                }));
    }

    public void getBanner() {
        addSubscription(RetrofitSource.createApi(HttpPostService.class).getBanner("2")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<BannerBean>>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<List<BannerBean>> response) {
                        iView.setBanner(response.getData());
                    }
                }));
    }

    public void getSystemNews() {
        addSubscription(RetrofitSource.createApi(HttpPostService.class).getSystemNews()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<SystemNoticeBean>>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<List<SystemNoticeBean>> response) {
                        iView.setSystemNews(response.getData());
                    }
                }));
    }


}
